/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * GuiDadosCliente.java
 *
 * Created on 22/11/2011, 22:18:55
 */
package view.gui;

import javax.swing.JOptionPane;
import model.dominio.Cliente;
import controller.ControllerCliente;

/**
 *
 * @author Braulio
 * Classe responsável por tratar os Dados de Cliente.
 * Este tratamento consiste no Cadastro ou na Edição de um Cliente.
 *
 */
public class GuiDadosCliente  extends JInternalFramePermissao {

    private Cliente cliente;

    /** Creates new form GuiDadosCliente */
    /**
     * Construtor da interface gráfica para Cadastrar novo Cliente.
     */
    public GuiDadosCliente() {
        initComponents();
        cliente = new Cliente();
    }

    /** Creates new form GuiDadosCliente */
    /**
     * Construtor da interface gráfica para Edição de Cliente previamente cadastrado.
     * @param c 
     * Parâmetro preenchido na busca, ao se clicar em uma linha, possibilitando edição do objeto.
     */
    public GuiDadosCliente(Cliente c) {
        initComponents();
        boxNome.setText(c.getNome());
        boxCpf.setText(c.getCpf());
        boxEnd.setText(c.getEndereco());
        boxFone.setText(c.getTelefone());
        jButton1.setText("Editar");
        cliente = c;
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jPanel1 = new javax.swing.JPanel();
        jLabel1 = new javax.swing.JLabel();
        jLabel2 = new javax.swing.JLabel();
        jLabel3 = new javax.swing.JLabel();
        jLabel4 = new javax.swing.JLabel();
        boxNome = new javax.swing.JTextField();
        boxEnd = new javax.swing.JTextField();
        boxCpf = new javax.swing.JFormattedTextField();
        boxFone = new javax.swing.JFormattedTextField();
        jButton1 = new javax.swing.JButton();
        Cancelar = new javax.swing.JButton();

        setClosable(true);
        setTitle("Cadastrar cliente");
        addInternalFrameListener(new javax.swing.event.InternalFrameListener() {
            public void internalFrameActivated(javax.swing.event.InternalFrameEvent evt) {
                formInternalFrameActivated(evt);
            }
            public void internalFrameClosed(javax.swing.event.InternalFrameEvent evt) {
            }
            public void internalFrameClosing(javax.swing.event.InternalFrameEvent evt) {
            }
            public void internalFrameDeactivated(javax.swing.event.InternalFrameEvent evt) {
            }
            public void internalFrameDeiconified(javax.swing.event.InternalFrameEvent evt) {
            }
            public void internalFrameIconified(javax.swing.event.InternalFrameEvent evt) {
            }
            public void internalFrameOpened(javax.swing.event.InternalFrameEvent evt) {
            }
        });
        getContentPane().setLayout(null);

        jPanel1.setBorder(javax.swing.BorderFactory.createTitledBorder("Dados do Cliente"));
        jPanel1.setLayout(null);

        jLabel1.setText("Nome*:");
        jPanel1.add(jLabel1);
        jLabel1.setBounds(20, 30, 59, 30);

        jLabel2.setText("CPF*:");
        jPanel1.add(jLabel2);
        jLabel2.setBounds(20, 70, 59, 30);

        jLabel3.setText("Endereço:");
        jPanel1.add(jLabel3);
        jLabel3.setBounds(20, 110, 59, 30);

        jLabel4.setText("Telefone:");
        jPanel1.add(jLabel4);
        jLabel4.setBounds(20, 150, 59, 30);

        boxNome.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                boxNomeActionPerformed(evt);
            }
        });
        jPanel1.add(boxNome);
        boxNome.setBounds(80, 30, 260, 30);

        boxEnd.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                boxEndActionPerformed(evt);
            }
        });
        jPanel1.add(boxEnd);
        boxEnd.setBounds(80, 110, 259, 30);

        try {
            boxCpf.setFormatterFactory(new javax.swing.text.DefaultFormatterFactory(new javax.swing.text.MaskFormatter("###.###.###-##")));
        } catch (java.text.ParseException ex) {
            ex.printStackTrace();
        }
        boxCpf.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                boxCpfActionPerformed(evt);
            }
        });
        jPanel1.add(boxCpf);
        boxCpf.setBounds(80, 70, 259, 30);

        try {
            boxFone.setFormatterFactory(new javax.swing.text.DefaultFormatterFactory(new javax.swing.text.MaskFormatter("(##) ####-####")));
        } catch (java.text.ParseException ex) {
            ex.printStackTrace();
        }
        boxFone.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                boxFoneActionPerformed(evt);
            }
        });
        jPanel1.add(boxFone);
        boxFone.setBounds(80, 150, 259, 30);

        getContentPane().add(jPanel1);
        jPanel1.setBounds(0, 0, 360, 200);

        jButton1.setIcon(new javax.swing.ImageIcon(getClass().getResource("/view/imagens/salvar.png"))); // NOI18N
        jButton1.setText("Cadastrar");
        jButton1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton1ActionPerformed(evt);
            }
        });
        getContentPane().add(jButton1);
        jButton1.setBounds(20, 210, 140, 60);

        Cancelar.setIcon(new javax.swing.ImageIcon(getClass().getResource("/view/imagens/cancelar.png"))); // NOI18N
        Cancelar.setText("Cancelar");
        Cancelar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                CancelarActionPerformed(evt);
            }
        });
        getContentPane().add(Cancelar);
        Cancelar.setBounds(180, 210, 150, 60);

        setBounds(0, 0, 374, 317);
    }// </editor-fold>//GEN-END:initComponents

    private void CancelarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_CancelarActionPerformed
        this.dispose();
}//GEN-LAST:event_CancelarActionPerformed

    private void guardar() {
        if (boxNome.getText().isEmpty() || boxCpf.getText().equals("   .   .   -  ")) { // TODO errado o segundo isEmpty
            JOptionPane.showMessageDialog(null, "Campo(s) obrigatório(s), não preenchido(s)!", "ERRO", JOptionPane.ERROR_MESSAGE);
            return;
        }
        cliente.setNome(boxNome.getText());
        cliente.setCpf(boxCpf.getText());
        cliente.setEndereco(boxEnd.getText());
        cliente.setTelefone(boxFone.getText());

        if (ControllerCliente.salvarCliente(cliente)) {
            JOptionPane.showMessageDialog(null, "Dados registrados com sucesso!", "Sucesso!", JOptionPane.INFORMATION_MESSAGE);
            this.dispose();
        } else {
            JOptionPane.showMessageDialog(null, "Falha ao registrar os dados!" + cliente.getCodigo(), "Falha!", JOptionPane.ERROR_MESSAGE);
        }
    }
    private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton1ActionPerformed
        guardar();
}//GEN-LAST:event_jButton1ActionPerformed

    private void formInternalFrameActivated(javax.swing.event.InternalFrameEvent evt) {//GEN-FIRST:event_formInternalFrameActivated
    }//GEN-LAST:event_formInternalFrameActivated

    private void boxNomeActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_boxNomeActionPerformed
        guardar();
    }//GEN-LAST:event_boxNomeActionPerformed

    private void boxCpfActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_boxCpfActionPerformed
        guardar();
    }//GEN-LAST:event_boxCpfActionPerformed

    private void boxEndActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_boxEndActionPerformed
        guardar();
    }//GEN-LAST:event_boxEndActionPerformed

    private void boxFoneActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_boxFoneActionPerformed
        guardar();
    }//GEN-LAST:event_boxFoneActionPerformed
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton Cancelar;
    private javax.swing.JFormattedTextField boxCpf;
    private javax.swing.JTextField boxEnd;
    private javax.swing.JFormattedTextField boxFone;
    private javax.swing.JTextField boxNome;
    private javax.swing.JButton jButton1;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JPanel jPanel1;
    // End of variables declaration//GEN-END:variables

    @Override
    public int getPermissao() {
        if(cliente.getCodigo()!=null && cliente.getCodigo()>0){
            return 7;
        }else{
            return 6;
        }
    }
}
